package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.ISaleOrderItemDAO;
import com.twentyOne.ClothingLand.vo.SaleOrderItem;

public class SaleOrderItemDAO extends BaseDAO implements ISaleOrderItemDAO {

	@Override
	public void addSaleOrderItem(SaleOrderItem item) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(item);
		tx.commit();
		session.close();
		
	}

	@Override
	public List<SaleOrderItem> searchItemBySaleOrderNo(String saleOrderNo) {
		
		Session session = getSession();
		String hql = "from SaleOrderItem as s where s.saleOrderNo = " + saleOrderNo;
		Query query = session.createQuery(hql);
		List<SaleOrderItem> list = query.list();
		session.close();
		if(list.size() != 0)
			return list;
		else 
			return null;
		
	}

	@Override
	public void modifySaleOrderItem(SaleOrderItem item) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update SaleOrderItem set quantity=:quantity, subTotal=:subTotal where saleOrderNo=:saleOrderNo and barcode=:barcode";
		Query query = session.createQuery(hql);
		query.setInteger("quantity", item.getQuantity());
		query.setDouble("subTotal", item.getSubTotal());
		query.setString("saleOrderNo", item.getSaleOrderNo());
		query.setString("barcode", item.getBarcode());
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public SaleOrderItem searchItemByNo_Barcode(String saleOrderNo,
			String barcode) {
		
		Session session = getSession();
		String hql = "from SaleOrderItem as s where s.saleOrderNo = " + saleOrderNo + " and s.barcode = " + barcode;
		Query query = session.createQuery(hql);
		List<SaleOrderItem> list = query.list();
		session.close();
		if(list.size() != 0)
			return list.get(0);
		else 
			return null;
		
	}

	@Override
	public void deleteSaleOrderItem(SaleOrderItem item) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "delete SaleOrderItem where id = " + item.getId();
		Query query = session.createQuery(hql);
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

}
